import constants from '@/constants';
import { BaseService } from '@/core/base.service';
import { getCode, halt } from '@/utils';
import { Injectable, Logger } from '@nestjs/common';
import { resolve } from 'path';
import { GeeTestService } from './geetest.service';
import { random } from 'radash';
import { ZrwinfoService } from '@/core/zrwinfo.service';

@Injectable()
export class VerifyService extends BaseService {
  constructor(
    private readonly geeTestService: GeeTestService,
    private readonly zrwinfoService: ZrwinfoService,
  ) {
    super();
  }

  public async sendSms(mobile: string) {
    const code = await getCode();
    Logger.debug(`短信验证码: ${mobile} - ${code}`);
    await this.redis.set(`verify:code:${mobile}`, code, 'EX', 180);
    if (!constants.isDev) {
      // await this.geeTestService.sendMessage(mobile, { code });
      await this.zrwinfoService.sendMessage(mobile, code);
    }
    return true;
  }

  public async verifyCode(mobile: string, code: string) {
    const val = await this.redis.get(`verify:code:${mobile}`);
    // if (!!val) {
    //   await this.redis.del(`verify:code:${mobile}`);
    // }
    return val === code;
  }

  public async getImage(mobile: string) {}

  public async verify(mobile: string, x: number) {
    const val = await this.redis.get(`verify:${mobile}`);
    if (!val) {
      return false;
    }
    await this.redis.del(`verify:${mobile}`);
    const xVal = Number(val);
    if (x >= xVal - 20 && x <= xVal + 20) {
      return true;
    }
    return false;
  }
}
